package com.lm.leetcode.arr;

import java.util.*;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 11:47 2025/5/7
 */
public class OneDimArrayDemo {
    public static void main(String[] args) {
        OneDimArrayDemo demo = new OneDimArrayDemo();
        //int[] arr = {1,3,4,6,4,3,1};
        //int[] arr = {-1,-1,0,0,-1,-1};
        //int flag = demo.pivotIndex(arr);
        //int[] arr = {0,1,2,4,5};
        //int flag = demo.searchInsert(arr,3);

    }

    //寻找中心索引 索引两边的元素相加之和相等
    public int pivotIndex(int[] nums) {
        int flag = -1;
        for (int i = 0; i < nums.length; i++) {
            int left = sum(nums, 0, i);
            int right = sum(nums, i + 1, nums.length);
            System.out.println(left + ":" + right);
            if (left == right) {
                flag = i;
                break;
            }
        }
        return flag;
    }

    public int sum(int[] nums, int start, int end) {
        int sum = 0;
        for (int i = start; i < end; i++) {
            sum += nums[i];
        }
        return sum;
    }

    //给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
    public int searchInsert(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            if (target == nums[i]) {
                return i;
            } else if (target < nums[i]) {
                for (int j = nums.length; j > i; j--) {
                    nums[j + 1] = nums[j];
                }
                nums[i] = target;
                return i;
            }
        }
        return -1;
    }



}
